package com.tan.androidtest.materialdesign;

import android.animation.Animator;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewPropertyAnimator;
import android.widget.TextView;

import com.tan.androidtest.R;
import com.tan.androidtest.util.SizeUtils;

/**
 * 测试CoordinatorLayout 和 FloatingActionButton(FAB)
 * 悬浮按钮
 */

public class TestFloatingActionButtonActivity extends AppCompatActivity implements View.OnClickListener {

    private FloatingActionButton mFabTest;
    private TextView mTvTest;
    private int mSavedOldScrollY = 0;
    private ViewPropertyAnimator hideAnimator;//隐藏的动画
    private boolean mHideAnimHasStarted = false;
    private ViewPropertyAnimator showAnimator;//隐藏的动画
    private boolean mShowAnimHasStarted = false;


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_floating_action_button_layout);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void init() {
        /*ScrollView nsv_test = findViewById(R.id.nsv_test);
        nsv_test.setOnClickListener(this);
        nsv_test.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                Log.i("==--", "oldScrollY: " + oldScrollY);
                Log.i("==--", "oldScrollY - mSavedOldScrollY: " + (oldScrollY - mSavedOldScrollY));
                if (oldScrollY - mSavedOldScrollY > 15) {//向上滑
                    hideFab();
                } else if (oldScrollY - mSavedOldScrollY < -15) {//向下滑
                    showFab();
                }
                mSavedOldScrollY = oldScrollY;


            }
        });*/
        mTvTest = findViewById(R.id.tv_test);
        //mTvTest.setOnClickListener(this);
        mFabTest = findViewById(R.id.fab_test);

    }

    /**
     * FAB的隐藏动画
     */
    private void hideFab() {
        if (mFabTest != null) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) mFabTest.getLayoutParams();
            int translationDistance = (int) SizeUtils.dpToPixel(layoutParams.bottomMargin) + mFabTest.getHeight();
            if (mFabTest.getVisibility() == View.VISIBLE && !mHideAnimHasStarted) {
                hideAnimator = mFabTest.animate().translationYBy(translationDistance).setDuration(500).setListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        mHideAnimHasStarted = true;
                        mFabTest.setEnabled(false);
                        mTvTest.setClickable(false);
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mFabTest.setVisibility(View.GONE);
                        mTvTest.setClickable(true);
                        mHideAnimHasStarted = false;
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
            }
        }
    }

    /**
     * FAB的显示动画
     */
    private void showFab() {
        if (mFabTest != null) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) mFabTest.getLayoutParams();
            int translationDistance = (int) SizeUtils.dpToPixel(layoutParams.bottomMargin) + mFabTest.getHeight();
            if (mFabTest.getVisibility() == View.GONE && !mShowAnimHasStarted) {
                showAnimator = mFabTest.animate().translationYBy(-translationDistance).setDuration(500).setListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        mFabTest.setVisibility(View.VISIBLE);
                        mTvTest.setClickable(false);
                        mShowAnimHasStarted = true;
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mFabTest.setVisibility(View.VISIBLE);
                        mTvTest.setClickable(true);
                        mShowAnimHasStarted = false;
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
            }

        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nsv_test: {
                if (mFabTest != null) {
                    if (mFabTest.getVisibility() == View.VISIBLE) {
                        mFabTest.animate().translationX(SizeUtils.dpToPixel(40));
                        mFabTest.setVisibility(View.GONE);
                    } else {
                        mFabTest.animate().translationX(-SizeUtils.dpToPixel(40));
                        mFabTest.setVisibility(View.VISIBLE);
                    }

                }
                break;
            }
            case R.id.tv_test: {
                /*if (mFabTest != null) {
                    ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) mFabTest.getLayoutParams();
                    int translationDistance = (int) SizeUtils.dpToPixel(layoutParams.bottomMargin) + mFabTest.getHeight();
                    if (mFabTest.getVisibility() == View.VISIBLE) {
                        mFabTest.animate().translationYBy(translationDistance).setDuration(500).setListener(new Animator.AnimatorListener() {
                            @Override
                            public void onAnimationStart(Animator animation) {
                                mFabTest.setEnabled(false);
                                mTvTest.setClickable(false);
                            }

                            @Override
                            public void onAnimationEnd(Animator animation) {
                                mFabTest.setVisibility(View.GONE);
                                mTvTest.setClickable(true);
                            }

                            @Override
                            public void onAnimationCancel(Animator animation) {

                            }

                            @Override
                            public void onAnimationRepeat(Animator animation) {

                            }
                        });
                    } else {
                        mFabTest.animate().translationYBy(-translationDistance).setDuration(500).setListener(new Animator.AnimatorListener() {
                            @Override
                            public void onAnimationStart(Animator animation) {
                                mFabTest.setVisibility(View.VISIBLE);
                                mTvTest.setClickable(false);
                            }

                            @Override
                            public void onAnimationEnd(Animator animation) {
                                mFabTest.setVisibility(View.VISIBLE);
                                mTvTest.setClickable(true);
                            }

                            @Override
                            public void onAnimationCancel(Animator animation) {

                            }

                            @Override
                            public void onAnimationRepeat(Animator animation) {

                            }
                        });

                    }

                }*/
                break;
            }
        }
    }
}
